package pure.engine.world.decorator 
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import pure.engine.world.IMonitor;
	import pure.engine.world.WorldBody;
	import pure.engine.core.ns_despair;
	import pure.engine.world.WorldManager;
	
	use namespace ns_despair;
	
	/**
	 * 具有滑鼠功能监视器
	 * @author Pure
	 * 
	 */
public class MouseMonitor extends MonitorDecorator
{
	public function MouseMonitor(baseMonitor:IMonitor)
	{
		super(baseMonitor);
		
		this.addEventListener(Event.ADDED_TO_STAGE, ____onAddedToStage, false, 0, true);
	}
	
	
	
	//======================
	// Const
	//======================
	
	
	private static var mouseEvents:Array =    // 10种滑鼠事件类型.
	[								
		//MouseEvent.CLICK,
		//MouseEvent.DOUBLE_CLICK,
		MouseEvent.MOUSE_DOWN, 
		MouseEvent.MOUSE_MOVE,
		//MouseEvent.MOUSE_OUT, 
		//MouseEvent.MOUSE_OVER, 
		MouseEvent.MOUSE_UP, 
		//MouseEvent.MOUSE_WHEEL, 
		//MouseEvent.ROLL_OUT, 
		//MouseEvent.ROLL_OVER
	];
	
	
	
	
	//======================
	// Event
	//======================
	
	
	/**
	 * 加入Stage
	 */
	private function ____onAddedToStage(e:Event):void
	{
		this.addEventListener(Event.REMOVED_FROM_STAGE, ____onRemovedFromStage, false, 0, true);
		
		var l:int = mouseEvents.length;
		
		while (--l > -1)
		{
			stage.addEventListener(mouseEvents[l], ____onMouseInteract );
		}
	}
	
	
	/**
	 * 从Stage移除
	 */
	private function ____onRemovedFromStage(e:Event):void
	{
		this.removeEventListener(Event.REMOVED_FROM_STAGE, ____onRemovedFromStage);
		
		var l:int = mouseEvents.length;
		
		while (--l > -1)
		{
			stage.removeEventListener(mouseEvents[l], ____onMouseInteract );
		}
	}
	

	/**
	 * 滑鼠交互
	 */
	private function ____onMouseInteract(e:MouseEvent):void
	{
		var mouseX:Number = this.mouseX;
		var mouseY:Number = this.mouseY;
		var worldBody:WorldBody = WorldManager.m_worldBodyList[this.m_name];
		
		if (mouseX > 0 && mouseX < m_viewW && mouseY > 0 && mouseY < m_viewH && worldBody.canInteract)
		{
			worldBody.updateInteractedState(e.type, mouseX, mouseY);
		}
	}

	
}

}